package ac.leedsmet.devmanager.core.persistence;

import com.thoughtworks.xstream.XStream;

import java.io.*;

/**
 * This class is storing objects in a binary file
 * @author Takamura
 * @author Samael
 */
public class FileStorage {

    public static void saveObjectToFile(Object object, String filename) throws IOException {
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(filename));
        outputStream.writeObject(object);
        System.out.println("file saved: " + filename);
        outputStream.close();
    }

    public static Object loadObjectFromFile(String filename) throws ClassNotFoundException, IOException {
        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(filename));
        return inputStream.readObject();
    }

    public static void exportToXmlFile(Object object, String filename) throws IOException {
        XStream xstream = new XStream();
        ObjectOutputStream outputStream = xstream.createObjectOutputStream(new FileOutputStream(filename));
        outputStream.writeObject(object);
        System.out.println("Data exported to XML file: " + filename);
        outputStream.close();
    }

    public static Object importFromXmlFile(String filename) throws IOException, ClassNotFoundException {
        XStream xstream = new XStream();
        ObjectInputStream inputStream = xstream.createObjectInputStream(new FileInputStream(filename));
        return inputStream.readObject();
    }
}
